<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Baidu
 *
 * @author Implement <hdjahs@qq.com>
 */
header("Content-type: text/html; charset=utf-8");
include_once BASE_PATH . '/Tool/Tool.php';
include_once BASE_PATH . '/Tool/lib/LIB_http.php';
include_once BASE_PATH . '/Tool/lib/LIB_parse.php';
include_once BASE_PATH . '/Tool/lib/LIB_resolve_addresses.php';
include_once BASE_PATH . '/Tool/lib/LIB_http_codes.php';

class Baidu {

    private $url = "";
    public $result = '';
    public $str = '';
    public $key = '';
    public $img = '';
    public $txtPath = '';
    public $tmpPath = '';
    public $imgPath = '';

    public function init($keys, &$ids) {
        $url = "http://baike.baidu.com/search?word=%s&pn=0&rn=0&enc=utf8";
        $this->createPath();
        foreach ($keys as $k => $key) {
            $this->str = '';
            $this->result = '';
            $this->img = '';
            $this->key = iconv('utf-8', 'gbk', $key);
            $key = urlencode($key);
            $this->url = sprintf($url, $key);
            if (!$this->isDown()) {
                $this->run();
                unset($ids[$k]);
            }
        }
    }

    public function createPath() {
        $Path = BASE_PATH . '/Log/author/';
        $this->txtPath = BASE_PATH . '/Log/author/txt/';
        $this->tmpPath = BASE_PATH . '/Log/author/tmp/';
        $this->imgPath = BASE_PATH . '/Log/author/img/';
        $tool = Tool::getIns();
        $tool->createPath($Path);
        $tool->createPath($this->txtPath);
        $tool->createPath($this->tmpPath);
        $tool->createPath($this->imgPath);
    }

    public function isDown() {
        $txtPath = $this->txtPath . $this->key . '.txt';
        $txtExistPath = $this->tmpPath . md5($txtPath) . '.txt';

        $imgPath = $this->imgPath . $this->key . '.jpg';
        $imgExistPath = $this->tmpPath . md5($imgPath) . '.txt';

        if (file_exists($txtExistPath) && file_exists($imgExistPath)) {
            return true;
        }
        return false;
    }

    public function run() {
        $this->getResult();
        $this->getSaveData();
        $obj = new SaveToFile($this);
        $obj->save();

        $obj = new SaveToFileByImg($this);
        $obj->save();
    }

    public function getSaveData() {
        if (!empty($this->result)) {
            new BaiduDecoratorTitle($this);
            new BaiduDecoratorDesc($this);
            new BaiduDecoratorImg($this);
        }
    }

    public function getResult() {                              
        $download_page = http_get($this->url, $ref = "");
        $regex = '/class="result-title" href\s*=\s*(?:"([^"]*)"|\'([^\']*)\'|([^"\'>\s]+))/';
        preg_match_all($regex, $download_page['FILE'], $link_array);
        if (isset($link_array[1][0])) {
            $download_page = http_get($link_array[1][0], $ref = "");
            $this->result = isset($download_page['FILE']) ? $download_page['FILE'] : '';
        }
    }

}
